package com.example.zpositionanimationdemo.slice;

import com.example.zpositionanimationdemo.ResourceTable;
import com.example.zpositionanimationdemo.adapter.HistoryItemProvider;
import com.example.zpositionanimationdemo.bean.HistoryBean;
import com.example.zpositionanimationdemo.chechbox.RCheckBox;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;

public class ProgrammaticallyAddSlice extends AbilitySlice {
    private RCheckBox optOutCheckbox;
    private Button add_but,clear_but;
    private ArrayList<HistoryBean> beans = new ArrayList<>();
    private ListContainer list;
    private HistoryItemProvider provider,itemProvider;
    private static boolean isCheck_but = false;
    int i = 0;

    public static boolean isIsCheck_but() {
        return isCheck_but;
    }

    public static void setIsCheck_but(boolean isCk_but) {
        ProgrammaticallyAddSlice.isCheck_but = isCk_but;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_programmaticallyadd);
        initView();
        initData();
        initClick();
    }

    private void initClick() {
        optOutCheckbox.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isBoolean) {
                absButton.setChecked(isBoolean);
                if (isIsCheck_but() == true){
                    setIsCheck_but(false);
                }else {
                    isCheck_but = true;
                }
                itemProvider.notifyDataChanged();
            }
        });

        add_but.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                i++;
                beans.add(new HistoryBean("Item " + i));
                itemProvider.notifyDataChanged();
            }
        });

        clear_but.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                i = 0;
                list.setItemProvider(provider);
                beans.clear();
                list.setItemProvider(itemProvider);
            }
        });
    }

    private void initData() {
    }

    private void initView() {
        optOutCheckbox = (RCheckBox) findComponentById(ResourceTable.Id_optOutCheckbox);
        add_but = (Button) findComponentById(ResourceTable.Id_add_but);
        clear_but = (Button) findComponentById(ResourceTable.Id_clear_but);
        list = (ListContainer) findComponentById(ResourceTable.Id_history);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(-13615201);
        itemProvider = new HistoryItemProvider(beans, ProgrammaticallyAddSlice.this, ResourceTable.Layout_item_last_request);
        list.setItemProvider(itemProvider);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        optOutCheckbox.setChecked(false);
        setIsCheck_but(false);
        i = 0;
        list.setItemProvider(provider);
        beans.clear();
        list.setItemProvider(itemProvider);
    }
}
